﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Studio77.CrazyJigsaw.Entities;
using Studio77.CrazyJigsaw.Core;
using System.Windows.Media.Imaging;

namespace Studio77.CrazyJigsaw.Pages
{
    public partial class StagePage : PhoneApplicationPage
    {
        public static StagePage CurrentStagePage;

        StageItemViewModelCollection stages = new StageItemViewModelCollection();


        public StagePage()
        {
            InitializeComponent();
            CurrentStagePage = this;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //s-风景 a-动物 c-卡通
            base.OnNavigatedTo(e);
            stages.Clear();
            string stageType = "s";
            if (!NavigationContext.QueryString.TryGetValue("StageType", out stageType))
            {
                stageType = StageManager.Instance.CurrentStageType;
            }
            else
            {
                StageManager.Instance.CurrentStageType = stageType;
            }

            Random r = new Random(DateTime.Now.Millisecond);
            for (int i = 1; i <= 15; i++)
            {
                StageItemViewModel model = new StageItemViewModel();
                model.Index = i;
                model.StageType = stageType;

                string code = stageType + "_" + i.ToString();

                //对应的图片
                model.StageImage = new BitmapImage(new Uri("/Images/gameImage/" + code + ".jpg",
                                                          UriKind.Relative));

                //读取完成情况
                int starCount = 0;
                if (StageManager.Instance.StageDatas.ContainsKey(code))
                {
                    StageData data = StageManager.Instance.StageDatas[code];
                    starCount = data.StageLevel;
                }


                model.NoStarVisibility = System.Windows.Visibility.Collapsed;
                model.OneStarVisibility = System.Windows.Visibility.Collapsed;
                model.TwoStarVisibility = System.Windows.Visibility.Collapsed;
                model.ThreeStarVisibility = System.Windows.Visibility.Collapsed;
                if (starCount == 0)
                {
                    model.NoStarVisibility = System.Windows.Visibility.Visible;  
                }
                else if (starCount == 1)
                {
                    model.OneStarVisibility = System.Windows.Visibility.Visible;  
                }
                else if (starCount == 2)
                {
                    model.TwoStarVisibility = System.Windows.Visibility.Visible;  
                }
                else if (starCount == 3)
                {
                    model.ThreeStarVisibility = System.Windows.Visibility.Visible;  
                }

                //生成最终角度
                int temp = r.Next(10);
                int side = 1;
                if (temp >= 5)
                {
                    side = -1;
                }
                model.AimRotated = (r.NextDouble() * 8 + 1) * side;

                stages.Add(model);
            }
            this.stageItemsControl.DataContext = stages;
        }
    }
}